﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using blackhouse.NemoBlog.Business;
using blackhouse.NemoBlog.Business.Presenter;
using blackhouse.NemoBlog.Business.View;
using blackhouse.WebNemo.Web;
using mojoPortal.Web;
using blackhouse.WebNemo.Business.Groups;
using blackhouse.JavaScript.JQuery;
using blackhouse.mojoPortalExtend;

namespace blackhouse.NemoBlog.Web {
    public partial class StandardNemoBlogModule : UserControl, IPostListView, INeedParentSiteModuleControl {

        #region Fields

        private int selectedTagId;
        private bool selectedTagIdParsed;
        private int lastPostId = -1;

        #endregion

        #region Properties

        public SiteModuleControl ParentControl {
            get;
            set;
        }


        public int SelectedTagId {
            get {
                if (!this.selectedTagIdParsed) {
                    this.selectedTagIdParsed = true;
                    Int32.TryParse(Request["tag"], out this.selectedTagId);
                }
                return this.selectedTagId;
            }
        }

        public int PlaceId {
            get { return this.ParentControl.ModuleId; }
        }

        public int PageId {
            get {
                return this.ParentControl.ModuleConfiguration.PageId;
            }
        }

        public bool IsPublisher {
            get {
                return this.pnlActionPanel.Visible;
            }
            set {
                if (this.pnlActionPanel.Visible != value)
                    this.pnlActionPanel.Visible = value;
            }
        }

        public bool BlogSubtitleVisible {
            get { return ltlBlogSecondName.Visible; }
            set { this.ltlBlogSecondName.Visible = value; }
        }

        public string BlogTitle {
            get { return this.ltlBlogName.Text; }
            set { this.ltlBlogName.Text = value; }
        }

        public string BlogSubtitle {
            get { return this.ltlBlogSecondName.Text; }
            set { this.ltlBlogSecondName.Text = value; }
        }

        public bool PostsVisible {
            get { return this.rptPosts.Visible; }
            set { this.rptPosts.Visible = value; }
        }

        public bool NoPostsVisible {
            get { return this.pnlNoPostsFind.Visible; }
            set { this.pnlNoPostsFind.Visible = value; }
        }

        public int LastPostId {
            get {
                if (this.lastPostId == -1) {
                    string sV = Request["lastId"];
                    int v = 0;
                    if (!String.IsNullOrEmpty(sV)
                        && Int32.TryParse(sV, out v))
                        this.lastPostId = v;
                    else
                        this.lastPostId = 0;
                }
                return this.lastPostId;
            }
        }

        public bool SeeMoreButtonVisible {
            get {
                return this.seeMoreLink.Visible;
            }
            set {
                this.seeMoreLink.Visible = value;
            }
        }

        public bool RequireSnapshoot {
            get {
                return Request.QueryString.AllKeys.Contains("_escaped_fragment_");
            }
        }

        #endregion

        #region Methods

        public string SuggestDefaultTitle() {
            return this.ParentControl.Title;
        }

        public void SetSettingsLink(IGroup settingsGroup, int recordId) {
            this.lteSettings.GroupSysName = settingsGroup.SystemName;
            this.lteSettings.RecordId = recordId;
        }

        public void SetAddNewLink(IGroup postsGroup, int pageId, int placeId) {
            this.lteAddPost.GroupSysName = postsGroup.SystemName;
            this.lteAddPostOnNoContent.RecordId = 0;
            this.lteAddPost.AddSpecialParameter("pageId", pageId.ToString());
            this.lteAddPost.AddSpecialParameter("mid", placeId.ToString());
        }

        public void SetAddNewLinkInNoPostContent(IGroup postsGroup, int pageId, int placeId) {
            this.lteAddPostOnNoContent.GroupSysName = postsGroup.SystemName;
            this.lteAddPostOnNoContent.RecordId = 0;
            this.lteAddPostOnNoContent.AddSpecialParameter("pageId", pageId.ToString());
            this.lteAddPostOnNoContent.AddSpecialParameter("mid", placeId.ToString());
        }

        public void PopulatePosts(NemoBlogPost[] postsToLoad) {
            this.rptPosts.DataSource = postsToLoad.Select(p => new DTNemoBlogPost(p));
            this.rptPosts.DataBind();//postsToLoad[0].Tags.Length
        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            this.seeMoreLink.Attributes.Add("data-action", "load-more-posts");
            JQueryLibraries jql = new JQueryLibraries();
#if DEBUG
            jql.IncludeFileBasedOnJQuery("blackhouse.NemoBlog.Web.ClientScript.lazzy.load.js,blackhouse.NemoBlog.Web");
#else
            jql.IncludeFileBasedOnJQuery("blackhouse.NemoBlog.Web.ClientScript.lazzy.load.min.js,blackhouse.NemoBlog.Web");
#endif

            mojoBasePage cmsPage = this.Page as mojoBasePage;
            if (cmsPage != null)
                cmsPage.AdditionalMetaMarkup = "<meta name=\"fragment\" content=\"!\">";
        }

        public void SetSeo(string title, string url) {
            PageSeo seo = new PageSeo(title, String.Empty, Request.Url.GetLeftPart(UriPartial.Authority) + PageProvider.AdjustUrlToCurrentSite(url));
            PageProvider pp = new PageProvider();
            pp.SetSeo(seo);
        }

        #endregion

    }
}